define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wq/content/index',
                    add_url: 'wq/content/add',
                    edit_url: 'wq/content/edit',
                    del_url: 'wq/content/del',
                    multi_url: 'wq/content/multi',
                    table: 'wq_content',
                }
            });

            var table = $("#table");

            console.log(Config.titles);

            var columns = [];
            columns.push({field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate});
            //columns.push({field: 'template.name', title: __('Template.name'), operate: false});
            for (var i = 0; i < Config.titles.length; i++) {
                columns.push({field: 'c' + i, title: Config.titles[i], operate: false});
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                //showExport: false,
                searchFormVisible: false,
                columns: columns
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_wq_content', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_wq_content')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wq_content')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        addnew: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});